<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/inc::header">
</head>
<body>
<input name="managerId" id="managerId" type="hidden"/>
<div class="zTreeDemoBackground left">
    <ul id="tree" class="ztree"></ul>
</div>
<button class="layui-btn" id="formSubmit" lay-submit="" lay-filter="formSubmit" style="display: none">立即提交</button>
<script th:replace="/common/inc::incJs"></script>
<script src="/js/ztree/jquery-1.4.4.min.js"></script>
<script src="/js/ztree/jquery.ztree.core.js"></script>
<script src="/js/ztree/jquery.ztree.excheck.js"></script>
<link rel="stylesheet" href="/css/ztree/zTreeStyle/zTreeStyle.css">
<script>
    var form = layui.form;
    var successCallback;
    var managerId = getQueryString("managerId");
    $("#managerId").val(managerId);

    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    var nodes = [];
    var nodeId;
    $(function () {
        $.post('/backend/security/menu/getList', function (res) {
            if (res.code < 0) {
                layer.msg(res.message);
            }
            else {
                for (var i = 0; i < res.body.length; i++) {
                    nodes.push({
                        id: res.body[i].id,
                        pId: res.body[i].parentId,
                        name: res.body[i].menuName,
                        open: true
                    });
                }
                $.fn.zTree.init($("#tree"), setting, nodes);
                var zTree_Menu = $.fn.zTree.getZTreeObj("tree");

                $.post('/backend/security/menuRight/list', {
                    managerId: managerId,
                    pageSize: 200
                }, function (reslust) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        for (var j = 0; j < reslust.body.length; j++) {
                            nodeId = zTree_Menu.getNodeByParam("id", reslust.body[j].menuId);
                            zTree_Menu.checkNode(nodeId);
                        }
                    }
                });
            }
        });
    });

    var formSubmit = function (callback) {
        successCallback = callback;
        $("#formSubmit").click();
    };

    form.on('submit(formSubmit)', function (data) {
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var node = treeObj.getCheckedNodes(true);
        var v = "";
        $.ajax({
            url: '/backend/security/menuRight/delMr',
            type: 'post',
            async: false,
            dataType: 'json',
            data: {"managerId": managerId},
            success: function (data) {
                if (data.code < 0) {
                    layer.msg(data.message);
                }
                else {
                    var list;
                    for (var i = 0; i < node.length; i++) {
                        $.post('/backend/security/menuRight/add', {
                            managerId: managerId,
                            menuId: node[i].id
                        }, function (res) {
                            if (res.code < 0) {
                                layer.msg(res.message);
                            }
                        });
                    }
                    layer.msg("授权成功");
                }
            }
        });
        return false;
    });
</script>
</body>
</html>